package com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Request;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.FileManage;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo.ITXingzhengfawenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.SubTypeInfo;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenFujian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwenFujianBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwenYijianBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwendengjibuBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.TXingzhengshouwendengjibuBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenFujian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenYijian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwendengjibu;
import com.tjsinfo.tjsoa.mail.bo.ITjsemailReceivedBo;
import com.tjsinfo.tjsoa.mail.bo.ITjsmailBo;
import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.workflow.Taskinstance;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workflow.bo.ITaskInstanceBO;

public class ShenPiAction extends DispatchAction {
	private ITXingzhengshouwenNeirongBO xingzhengshouwenNeirongBO;
	private ITXingzhengshouwenFujianBO xingzhengshouwenFujianBO;
	private ITXingzhengshouwenYijianBO xingzhengshouwenYijianBO;
	private ITXingzhengshouwendengjibuBO xingzhengshouwendengjibuBO;
	private ITaskInstanceBO taskInstanceBO;
	private ISystemUserBo systemUserBo;

	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public ITaskInstanceBO getTaskInstanceBO() {
		return taskInstanceBO;
	}

	public void setTaskInstanceBO(ITaskInstanceBO taskInstanceBO) {
		this.taskInstanceBO = taskInstanceBO;
	}

	public ITXingzhengshouwenNeirongBO getXingzhengshouwenNeirongBO() {
		return xingzhengshouwenNeirongBO;
	}

	public void setXingzhengshouwenNeirongBO(
			ITXingzhengshouwenNeirongBO xingzhengshouwenNeirongBO) {
		this.xingzhengshouwenNeirongBO = xingzhengshouwenNeirongBO;
	}

	public ITXingzhengshouwenFujianBO getXingzhengshouwenFujianBO() {
		return xingzhengshouwenFujianBO;
	}

	public void setXingzhengshouwenFujianBO(
			ITXingzhengshouwenFujianBO xingzhengshouwenFujianBO) {
		this.xingzhengshouwenFujianBO = xingzhengshouwenFujianBO;
	}

	public ITXingzhengshouwenYijianBO getXingzhengshouwenYijianBO() {
		return xingzhengshouwenYijianBO;
	}

	public void setXingzhengshouwenYijianBO(
			ITXingzhengshouwenYijianBO xingzhengshouwenYijianBO) {
		this.xingzhengshouwenYijianBO = xingzhengshouwenYijianBO;
	}

	/**
	 * 行政收文登记
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward dengji(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		MessageResources message = getResources(arg2);
		// 提交上传附件
		String fromFilePath = message.getMessage(arg2.getLocale(),
				"system.uploadFile.tempFilePath");
		String toFilePath = ToolKit
				.getProperties(
						"com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource.properties",
						this).getProperty("xingzhengshouwen.fileurl");
		List<Files> fileList = DealString.fileCopyTofile(arg2, fromFilePath,
				toFilePath);
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = new TXingzhengshouwenNeirong();
		xingzhengshouwenNeirong.setShouwenbianhao(arg2.getParameter("shouwenbianhao"));
		xingzhengshouwenNeirong.setShouwenriqi(ToolKit.FormatDate(arg2.getParameter("shouwenriqi")));
		xingzhengshouwenNeirong.setLaiwenzihao(arg2.getParameter("laiwenhao"));
		xingzhengshouwenNeirong.setLaiwenriqi(ToolKit.FormatDate(arg2.getParameter("laiwenriqi")));
		xingzhengshouwenNeirong.setLaiwenjiguan(arg2.getParameter("laiwendanwei"));
		xingzhengshouwenNeirong.setZhaiyou(arg2.getParameter("zhaiyao"));
		xingzhengshouwenNeirong.setNibanren(SessionUser.getUser(arg2).getUsername()); // 登记人
		xingzhengshouwenNeirong.setJinjichengdu(arg2.getParameter("jinjichengdu")); // 紧急程度
		xingzhengshouwenNeirong.setFengshu(arg2.getParameter("fenshu")); // 份数
		xingzhengshouwenNeirong.setShouwentype(arg2.getParameter("shouwentype"));
		String qingbanrenid = arg2.getParameter("qingbanid"); // 办公室主任批办
		String docid = xingzhengshouwenNeirongBO.addTXingzhengshouweineirong(
				xingzhengshouwenNeirong, qingbanrenid, fileList,SessionUser.getUserId(arg2));
		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		// 添加收文登记簿内容
		TXingzhengshouwendengjibu xingzhengshouwendengjibu = new TXingzhengshouwendengjibu();
		xingzhengshouwendengjibu.setDocid(docid);
		xingzhengshouwendengjibu.setLaiwendanwei(arg2.getParameter("laiwendanwei"));
		xingzhengshouwendengjibu.setMiji("");
		xingzhengshouwendengjibu.setNeirong("");
		xingzhengshouwendengjibu.setFenshu(arg2.getParameter("fenshu"));
		xingzhengshouwendengjibu.setQuxiang("");
		xingzhengshouwendengjibu.setShouwenbianhao(arg2.getParameter("zongshouwenhao"));
		xingzhengshouwendengjibu.setShouwenriqi(ToolKit.FormatDate(arg2.getParameter("shouwenriqi")));
		if (arg2.getParameter("laiwenhao") == null
				|| "".equals(arg2.getParameter("laiwenhao"))) {
			xingzhengshouwendengjibu.setYuanwenbianhao("");
		} else {
			xingzhengshouwendengjibu.setYuanwenbianhao(arg2.getParameter("laiwenhao"));
		}
		xingzhengshouwendengjibuBO.addTxingzhengshouwendangjibu(xingzhengshouwendengjibu);
		arg2.setAttribute("linkurl","tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message","com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 无流程行政收文登记
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward wuliuchengdengji(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		MessageResources message = getResources(arg2);
		// 提交上传附件
		String fromFilePath = message.getMessage(arg2.getLocale(),
				"system.uploadFile.tempFilePath");
		String toFilePath = ToolKit
				.getProperties(
						"com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource.properties",
						this).getProperty("xingzhengshouwen.fileurl");
		List<Files> fileList = DealString.fileCopyTofile(arg2, fromFilePath,
				toFilePath);
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = new TXingzhengshouwenNeirong();
		xingzhengshouwenNeirong.setShouwenbianhao(arg2
				.getParameter("zongshouwenhao"));
		xingzhengshouwenNeirong.setShouwenriqi(ToolKit.FormatDate(arg2
				.getParameter("shouwenriqi")));
		xingzhengshouwenNeirong.setLaiwenzihao(arg2.getParameter("laiwenhao"));
		xingzhengshouwenNeirong.setLaiwenriqi(ToolKit.FormatDate(arg2
				.getParameter("laiwenriqi")));
		xingzhengshouwenNeirong.setLaiwenjiguan(arg2
				.getParameter("laiwendanwei"));
		xingzhengshouwenNeirong.setZhaiyou(arg2.getParameter("zhaiyao"));
		xingzhengshouwenNeirong.setNibanren(SessionUser.getUser(arg2)
				.getUsername());
		String qingbanrenid = arg2.getParameter("qingbanid");
		String docid = xingzhengshouwenNeirongBO.addTXingzhengshouweineirong(
				xingzhengshouwenNeirong, qingbanrenid, fileList,
				SessionUser.getUserId(arg2));
		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		
		
		TXingzhengshouwendengjibu xingzhengshouwendengjibu = new TXingzhengshouwendengjibu();
		xingzhengshouwendengjibu.setDocid(docid);
		xingzhengshouwendengjibu.setLaiwendanwei(arg2
				.getParameter("laiwendanwei"));
		xingzhengshouwendengjibu.setMiji("");
		xingzhengshouwendengjibu.setNeirong("");
		xingzhengshouwendengjibu.setFenshu("");
		xingzhengshouwendengjibu.setQuxiang("");
		xingzhengshouwendengjibu.setShouwenbianhao(arg2
				.getParameter("zongshouwenhao"));
		xingzhengshouwendengjibu.setShouwenriqi(ToolKit.FormatDate(arg2
				.getParameter("shouwenriqi")));
		if (arg2.getParameter("laiwenhao") == null
				|| "".equals(arg2.getParameter("laiwenhao"))) {
			xingzhengshouwendengjibu.setYuanwenbianhao("");
		} else {
			xingzhengshouwendengjibu.setYuanwenbianhao(arg2
					.getParameter("laiwenhao"));
		}

		xingzhengshouwendengjibuBO
				.addTxingzhengshouwendangjibu(xingzhengshouwendengjibu);
		xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(docid);
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		if (workFlowTemplate.processInstanceEnd(Long
				.valueOf(xingzhengshouwenNeirong.getFlowid().toString()))) {
			List<TaskInstance> list = new ArrayList<TaskInstance>(
					workFlowTemplate.selectunfinishedTasks(Long
							.valueOf(xingzhengshouwenNeirong.getFlowid()
									.toString())));
			for (TaskInstance taskInstance : list) {
				xingzhengshouwenNeirongBO.deleteTask(Integer.parseInt(Long
						.valueOf(taskInstance.getId()).toString()));

			}

		}
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 控制
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward kongzhi(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		Long id = Long.valueOf(arg2.getParameter("taskid"));
		Long flowidL = xingzhengshouwenNeirong.getFlowid().longValue();
		arg2.setAttribute("taskid", id);
		TaskInstance taskInstance = workFlowTemplate.nextStep(id);
		if (taskInstance.getDescription().equals("办公室主任批办")) {
			return arg0.findForward("bangongshipiban");
		} else if (taskInstance.getDescription().equals("承办部门拟办")) {
			return arg0.findForward("bumenniban");
		} else if (taskInstance.getDescription().equals("领导批办")) {
			return arg0.findForward("lingdaopiban");
		} else if (taskInstance.getDescription().equals("承办部门承办")) {
			if(systemUserBo.isUserDuty(ToolKit.getSessionUser(arg2).getUser_id(), "办公室主任")){
				return arg0.findForward("bangongshichuanyue");
			}else{
				return arg0.findForward("bumenchengban");
			}
		} else if (taskInstance.getDescription().equals("办公室主任修改")) {
			return arg0.findForward("bangongshixiugai");
		} else if (taskInstance.getDescription().equals("传阅")) {
			return arg0.findForward("chuanyue");
		} else if (taskInstance.getDescription().equals("登记修改")) {
			return arg0.findForward("dengjixiugai");
		}
		return arg0.findForward("success");
	}

	/**
	 * 办公室主任批办
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward bangongshipiban(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = addXingzhengshouwenyijian(
				arg2, "办公室主任批办");
		String taskid = arg2.getParameter("taskid");
		Long taskidL = Long.valueOf(taskid);
		Long flowidL = xingzhengshouwenNeirong.getFlowid().longValue();
		
		Integer path = Integer.parseInt(arg2.getParameter("path"));
		if (path == 1) {
			//获取承办部门id list
			String chengbanbumenid=arg2.getParameter("chengbanbumenid");
			String chuandifangshibumen = arg2.getParameter("chuandifangshibumen");
			
			//获取分管领导id list
			String fenguanlingdaoid = arg2.getParameter("fenguanlingdaoid");
			String chuandifangshilingdao = arg2.getParameter("chuandifangshilingdao");
			setIdlistVariable(fenguanlingdaoid,"领导批办",flowidL,chuandifangshilingdao,workFlowTemplate);
			
			//获取办理部门id list
			String banlibumenid=arg2.getParameter("banlibumenid");
			String chuandifangshibanli = arg2.getParameter("chuandifangshibanli");
			setIdlistVariable(banlibumenid,"承办部门承办",flowidL,chuandifangshibanli,workFlowTemplate);
			
			//保存承办部门、领导名称列表、ld列表
			xingzhengshouwenNeirong.setBumennames(arg2.getParameter("chengbanbumenname"));
			xingzhengshouwenNeirong.setBumenids(chengbanbumenid);
			xingzhengshouwenNeirong.setLingdaonames(arg2.getParameter("fenguanlingdaoname"));
			xingzhengshouwenNeirong.setLingdaoids(fenguanlingdaoid);
			xingzhengshouwenNeirongBO.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
			
			//获取办公室主任用户，将其添加到部门承办
			List<TSystemUser> systemuserlist = systemUserBo.selectByDutyName("办公室主任");
			if (systemuserlist != null &&systemuserlist.size() > 0) {
				String bangongshiid = String.valueOf(systemuserlist.get(0).getUserId());
				appendlistVariable(bangongshiid, "承办部门承办", flowidL, taskidL, workFlowTemplate);
			} 
			
			//获取是否承办部门拟办
			String ischengbanniban=arg2.getParameter("ischengbanniban");
			if("1".equals(ischengbanniban)){  
				setIdlistVariable(chengbanbumenid,"承办部门拟办",flowidL,chuandifangshibumen,workFlowTemplate);
				workFlowTemplate.nextStep(taskidL,"to 承办部门拟办");
			}else{
				workFlowTemplate.nextStep(taskidL,"to 领导批办");
			}
		} else {
			workFlowTemplate.nextStep(taskidL, "to 登记修改");
		}

		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		arg2.setAttribute("linkurl","tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message","com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
/** 将获取的字符串型 id串 转换成list 并传入相应的流程变量中
 * 
 * isshunxu 传递方式： 1 顺序 0分发
 */
	private void setIdlistVariable(String idString,String variablename,Long flowidL, String isshunxu,WorkFlowTemplate workFlowTemplate){
	List<String> idlist = new ArrayList<String>();
	String[] idarray = null;
	if (idString == null ||"".equals(idString)) {
		idarray = new String[] {};
		workFlowTemplate.setVariable(variablename, null,flowidL);
	} else {
		idarray = idString.split(",");
		if ("1".equals(isshunxu)){
			List<String> listone = new ArrayList<String>();
			listone.add(idarray[0]);
			workFlowTemplate.setVariable(variablename, listone,flowidL);   //顺序时将第一个用户id传递给环节变量
			for (int i = 1; i < idarray.length; i++) {
				if (idarray[i] != null && !"".equals(idarray[i])) {
					idlist.add(idarray[i]);
				}
			}
			workFlowTemplate.setVariable(variablename+"列表", idlist,flowidL);  //其余用户id保存到环节列表变量中
		}else{
			for (int i = 0; i < idarray.length; i++) {
				if (idarray[i] != null && !"".equals(idarray[i])) {
					idlist.add(idarray[i]);
				}
			}
			workFlowTemplate.setVariable(variablename, idlist,flowidL);  //分发时直接将list传递给环节变量
		}
	}
	workFlowTemplate.setVariable(variablename+"传递",isshunxu,flowidL);  //传递方式： 1 顺序 0分发
}
	
/**
 * 顺序传递时为下一位任务执行人创建task，并列表变量
 * @param variablename
 * @param flowidL
 * @param taskidL
 * @param workFlowTemplate
 */
	private void updateIdlistVariable(String variablename,Long flowidL,Long taskidL,WorkFlowTemplate workFlowTemplate){
	Object liebiao=workFlowTemplate.getVariable(variablename+"列表", flowidL);
	if(liebiao!=null){
		List<String> templist = (List<String>)liebiao;
		if (templist.size()>0){
			String actorid=templist.get(0);
			if(!"".equals(actorid)&&actorid!=null){
				workFlowTemplate.createTaskInstance(taskidL,actorid);
			}
		}
		if(templist.size()>1){
			List<String> newlist = new ArrayList<String>();
			for (int i = 1; i < templist.size(); i++) {
				newlist.add(templist.get(i));
			}
			workFlowTemplate.setVariable(variablename+"列表",newlist ,flowidL);
		}else{
			workFlowTemplate.setVariable(variablename+"列表",null ,flowidL);
		}
	}
}

	/**
	 * 承办部门拟办
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward bumenniban(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = addXingzhengshouwenyijian(
				arg2, "承办部门拟办");
		String taskid = arg2.getParameter("taskid");
		Long taskidL = Long.valueOf(taskid);
		Long flowidL = xingzhengshouwenNeirong.getFlowid().longValue();
		
			//获取分管领导id list
			String fenguanlingdaoid = arg2.getParameter("fenguanlingdaoid");
			String chuandifangshilingdao = (String)workFlowTemplate.getVariable("领导批办传递",flowidL);
			setIdlistVariable(fenguanlingdaoid,"领导批办",flowidL,chuandifangshilingdao,workFlowTemplate);
			//获取办公室主任用户，将其添加到传阅
			List<TSystemUser> systemuserlist = systemUserBo.selectByDutyName("办公室主任");
			if (systemuserlist != null &&systemuserlist.size() > 0) {
				String bangongshiid = String.valueOf(systemuserlist.get(0).getUserId());
				appendlistVariable(bangongshiid, "传阅", flowidL, taskidL, workFlowTemplate);
			} 
			
			//保存领导名称列表、ld列表
			xingzhengshouwenNeirong.setLingdaonames(arg2.getParameter("fenguanlingdaoname"));
			xingzhengshouwenNeirong.setLingdaoids(fenguanlingdaoid);
			xingzhengshouwenNeirongBO.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
			
			String chengbanrenid=arg2.getParameter("chengbanrenid");
			if(!"".equals(chengbanrenid)&&null!=chengbanrenid){ //如果承办人指定了本部门其他承办人，则为该承办人增加taskinstance
				workFlowTemplate.createTaskInstance(taskidL,chengbanrenid);
			}else{				
				if("1".equals(workFlowTemplate.getVariable("承办部门拟办传递", flowidL))){  //如果顺序传递，为下一位承办人创建task，并更新承办部门拟办列表变量
					updateIdlistVariable("承办部门拟办",flowidL,taskidL,workFlowTemplate);   
				}
			}
			workFlowTemplate.nextStep(taskidL,"to 领导批办");
	
		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
	/**
	 * 领导批办
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward lingdaopiban(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = addXingzhengshouwenyijian(
				arg2, "领导批办");
		String taskid = arg2.getParameter("taskid");
		Long taskidL = Long.valueOf(taskid);
		Long flowidL = xingzhengshouwenNeirong.getFlowid().longValue();
		
		Integer path = Integer.parseInt(arg2.getParameter("path"));
		String chuandilingdaopiban=(String)workFlowTemplate.getVariable("领导批办传递", flowidL);
		if (path==1){
			//获取增加批办领导id
			String lingdaoid = arg2.getParameter("lingdaoid");
			String lingdaoname = arg2.getParameter("lingdaoname");
			String lingdaoidold = ","+xingzhengshouwenNeirong.getLingdaoids();
			if (!"".equals(lingdaoid)&& lingdaoid!=null  && lingdaoidold.indexOf(","+lingdaoid+",")<0){  //如果增加领导不在现存领导列表中
				if("1".equals(chuandilingdaopiban)){  //如果为顺序传递，将该领导增加在领导列表最后
					appendlistVariable(lingdaoid,"领导批办列表",flowidL,taskidL,workFlowTemplate);
				}else{		//若为分支传递，则为该领导创建task
					workFlowTemplate.createTaskInstance(taskidL,lingdaoid);
				}
				xingzhengshouwenNeirong.setLingdaoids(xingzhengshouwenNeirong.getLingdaoids()+lingdaoid+",");
				xingzhengshouwenNeirong.setLingdaonames(xingzhengshouwenNeirong.getLingdaonames()+ "," +lingdaoname);
				xingzhengshouwenNeirongBO.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
			}
			if("1".equals(chuandilingdaopiban)){
				updateIdlistVariable("领导批办",flowidL,taskidL,workFlowTemplate);
			};
			workFlowTemplate.nextStep(taskidL,"to 承办部门承办");
		}else{
			List<TSystemUser> systemuserlist = systemUserBo.selectByDutyName("办公室主任");
			if (systemuserlist != null &&systemuserlist.size() > 0) {
				workFlowTemplate.setVariable("办公室主任修改",systemuserlist.get(0).getUserId(),flowidL);
			}else{
				workFlowTemplate.setVariable("办公室主任修改",1,flowidL);
			}
			workFlowTemplate.nextStep(taskidL,"to 办公室主任修改");
		}
	
		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
	/**
	 * 承办部门承办
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward bumenchengban(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = addXingzhengshouwenyijian(
				arg2, "承办部门承办");
		String taskid = arg2.getParameter("taskid");
		Long taskidL = Long.valueOf(taskid);
		Long flowidL = xingzhengshouwenNeirong.getFlowid().longValue();
		String chengbanrenids=arg2.getParameter("chengbanrenids");
		if(!"".equals(chengbanrenids)&&null!=chengbanrenids){ //如果承办人指定了本部门其他承办人，则为该承办人增加taskinstance
			String[] idarray = null;
			idarray = chengbanrenids.split(",");
			for (int i = 0; i < idarray.length; i++) {
				if (idarray[i] != null && !"".equals(idarray[i])) {
					workFlowTemplate.createTaskInstance(taskidL,idarray[i]);
				}
			}
		}else{				
			if("1".equals(workFlowTemplate.getVariable("承办部门承办传递", flowidL))){ //顺序传递
				updateIdlistVariable("承办部门承办",flowidL,taskidL,workFlowTemplate);  //获取下一位承办人，并更新承办列表
			};
		}
		workFlowTemplate.nextStep(taskidL,"to 结束");
		addJingShouRen(arg2, xingzhengshouwenNeirong.getId());
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
	/**
	 * 办公室传阅
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward bangongshichuanyue(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		addXingzhengshouwenyijian(arg2, "承办部门承办");
		String taskid = arg2.getParameter("taskid");
		Long taskidL = Long.valueOf(taskid);
		Long flowidL = Long.valueOf(arg2.getParameter("flowid"));
		String banlibumenid=arg2.getParameter("banlibumenid");
		if (!"".equals(banlibumenid) && null!=banlibumenid){
			String[]  idarray = banlibumenid.split(",");
			for (int i = 0; i < idarray.length; i++) {
				if (idarray[i] != null && !"".equals(idarray[i])) {
					workFlowTemplate.createTaskInstance(taskidL, idarray[i]);
				}
			}
		}
		addJingShouRen(arg2, arg2.getParameter("id"));
		workFlowTemplate.nextStep(taskidL,"to 结束");
		arg2.setAttribute("linkurl","tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message","com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
	/**
	 * 流程变量 列表变量追加用户id
	 * @param ids
	 * @param variablename
	 * @param flowidL
	 * @param taskidL
	 * @param workFlowTemplate
	 */
	private void appendlistVariable(String idString,String variablename, Long flowidL, Long taskidL,WorkFlowTemplate workFlowTemplate) {
		// TODO Auto-generated method stub
		Object liebiao=workFlowTemplate.getVariable(variablename, flowidL);
		List<String> templist = new ArrayList<String>();
		if(liebiao!=null){
			templist = (List<String>)liebiao;
		}	
			if (idString != null && !"".equals(idString)){
				String[] idarray = null;
				idarray = idString.split(",");
				for (int i = 0; i < idarray.length; i++) {
					if (idarray[i] != null && !"".equals(idarray[i]) && !templist.contains(idarray[i])) {
						templist.add(idarray[i]);
					}
				}
				workFlowTemplate.setVariable(variablename,templist ,flowidL);
			}
		}

	/**
	 * 登记修改
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward dengjixiugai(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		String taskid = arg2.getParameter("taskid");
		MessageResources message = getResources(arg2);
		// 提交上传附件
		String fromFilePath = message.getMessage(arg2.getLocale(),
				"system.uploadFile.tempFilePath");
		String toFilePath = ToolKit
				.getProperties(
						"com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource.properties",
						this).getProperty("xingzhengshouwen.fileurl");
		List<Files> fileList = DealString.fileCopyTofile(arg2, fromFilePath,
				toFilePath);
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		xingzhengshouwenNeirong.setShouwenbianhao(arg2
				.getParameter("zongshouwenhao"));
		xingzhengshouwenNeirong.setShouwenriqi(ToolKit.FormatDate(arg2
				.getParameter("shouwenriqi")));
		xingzhengshouwenNeirong.setLaiwenzihao(arg2.getParameter("laiwenhao"));
		xingzhengshouwenNeirong.setLaiwenjiguan(arg2
				.getParameter("laiwendanwei"));
		xingzhengshouwenNeirong.setZhaiyou(arg2.getParameter("zhaiyao"));
		xingzhengshouwenNeirongBO
				.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
		if ("1".equals(arg2.getParameter("isdelete"))) {
			List<TXingzhengshouwenFujian> xingzhengshouwenFujianList = xingzhengshouwenFujianBO
					.selectByDocId(xingzhengshouwenNeirong.getId());
			for (TXingzhengshouwenFujian xingzhengshouwenFujian : xingzhengshouwenFujianList) {
				xingzhengshouwenFujianBO.deleteFujian(xingzhengshouwenFujian);
			}

		} else {

		}
		for (Iterator iterator = fileList.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TXingzhengshouwenFujian xingzhengshouwenFujian = new TXingzhengshouwenFujian();
			xingzhengshouwenFujian.setFujianAddress(file.toFilePath);
			xingzhengshouwenFujian.setFujianName(file.getOldFileName());
			xingzhengshouwenFujian.setFujianZhengname(file.getNewFileName());
			xingzhengshouwenFujian
					.setXingzhengshouwenNeirong(xingzhengshouwenNeirong);
			xingzhengshouwenFujianBO.addFujian(xingzhengshouwenFujian);

		}

		workFlowTemplate.nextStep(Long.valueOf(taskid), null);
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 进入修改
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward xiugai(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("xiugai");
	}

	/**
	 * 进入修改附件
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward xiugaifujian(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("xiugaifujian");
	}

	/**
	 * 修改附件
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward xiugaifujiansubmit(ActionMapping arg0,
			ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		List<TXingzhengshouwenFujian> list = new ArrayList<TXingzhengshouwenFujian>(
				xingzhengshouwenNeirong.getTXingzhengshouwenFujians());
		for (TXingzhengshouwenFujian xingzhengshouwenFujian : list) {
			xingzhengshouwenFujianBO.deleteFujian(xingzhengshouwenFujian);
		}
		MessageResources message = getResources(arg2);
		String fromFilePath = message.getMessage(arg2.getLocale(),
				"system.uploadFile.tempFilePath");
		String toFilePath = ToolKit
				.getProperties(
						"com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource.properties",
						this).getProperty("xingzhengshouwen.fileurl");
		List<Files> fileList = DealString.fileCopyTofile(arg2, fromFilePath,
				toFilePath);
		for (Iterator iterator = fileList.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TXingzhengshouwenFujian xingzhengshouwenFujian = new TXingzhengshouwenFujian();
			xingzhengshouwenFujian.setFujianAddress(file.toFilePath);
			xingzhengshouwenFujian.setFujianName(file.getOldFileName());
			xingzhengshouwenFujian.setFujianZhengname(file.getNewFileName());
			xingzhengshouwenFujian
					.setXingzhengshouwenNeirong(xingzhengshouwenNeirong);
			xingzhengshouwenFujianBO.addFujian(xingzhengshouwenFujian);

		}
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 修改
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward xiugaisubmit(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		xingzhengshouwenNeirong.setShouwenbianhao(arg2
				.getParameter("zongshouwenhao"));
		xingzhengshouwenNeirong.setShouwenriqi(ToolKit.FormatDate(arg2
				.getParameter("shouwenriqi")));
		xingzhengshouwenNeirong.setLaiwenzihao(arg2.getParameter("laiwenhao"));
		xingzhengshouwenNeirong.setLaiwenjiguan(arg2
				.getParameter("laiwendanwei"));
		xingzhengshouwenNeirong.setZhaiyou(arg2.getParameter("zhaiyao"));
		xingzhengshouwenNeirongBO
				.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 传阅
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward chuanyue(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub

		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		String taskid = arg2.getParameter("taskid");
		workFlowTemplate.nextStep(Long.valueOf(taskid), null);
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}


	/**
	 * 添加意见
	 * 
	 * @param arg2
	 * @param 环节
	 * @return
	 */
	private TXingzhengshouwenNeirong addXingzhengshouwenyijian(
			HttpServletRequest arg2, String huanjian) {
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		TXingzhengshouwenYijian xingzhengshouwenYijian = new TXingzhengshouwenYijian();
		xingzhengshouwenYijian.setExamineDate(new Date());
		Integer userid = SessionUser.getUserId(arg2);
		xingzhengshouwenYijian.setExamineUserId(userid);
		xingzhengshouwenYijian.setExamineUserName(SessionUser.getUser(arg2).getUsername());
		
		Integer orderno = systemUserBo.findByIdSystemUser(userid).getOrderNo(); 
		xingzhengshouwenYijian.setOrderNo(orderno);
		xingzhengshouwenYijian.setFlowId(xingzhengshouwenNeirong.getFlowid());
		xingzhengshouwenYijian.setFlowName(huanjian);
		xingzhengshouwenYijian.setCode(arg2.getParameter("code"));
		xingzhengshouwenYijian.setYuanwen(arg2.getParameter("yuanwen"));
		if (arg2.getParameter("yijian") == null
				|| "".equals(arg2.getParameter("yijian"))) {
			xingzhengshouwenYijian.setOpinion("");
		} else {
			xingzhengshouwenYijian.setOpinion(arg2.getParameter("yijian"));
		}
		xingzhengshouwenYijian
				.setXingzhengshouwenNeirong(xingzhengshouwenNeirong);
		xingzhengshouwenYijianBO
				.addXingzhengshouwenyijian(xingzhengshouwenYijian);
		return xingzhengshouwenNeirong;
	}

	/**
	 * 下载
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward download(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		String url = "error";
		InputStream fis = null;
		OutputStream toClient = null;
		// TODO Auto-generated method stub
		try {
			String id = arg2.getParameter("id");
			TXingzhengshouwenFujian xingzhengshouwenFujian = xingzhengshouwenFujianBO
					.selectById(id);
			String desfile = xingzhengshouwenFujian.getFujianAddress()
					.substring(
							0,
							xingzhengshouwenFujian.getFujianAddress()
									.lastIndexOf("\\"))
					+ "\\" + xingzhengshouwenFujian.getFujianName();
			desfile = ToolKit.delHTMLTag(desfile);
			ToolKit.copyFile(xingzhengshouwenFujian.getFujianAddress(), desfile);
			File file = new File(desfile);
			String filename = file.getName();
			if (filename.indexOf(".") == -1) {
				filename = filename + ".doc";
			} else {
				;
			}
			fis = new BufferedInputStream(new FileInputStream(desfile));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);

			arg3.reset();
			arg3.addHeader("Content-Disposition", "attachment;filename="
					+ new String(filename.getBytes(), "iso8859-1"));
			// arg3.addHeader("Content-Disposition ", "attachment;filename="+
			// new
			// String(xingzhengfawenFujian.getFujianName().getBytes(),"GBK"));
			arg3.addHeader("Content-Length", "" + file.length());
			toClient = new BufferedOutputStream(arg3.getOutputStream());
			arg3.setContentType("application/octet-stream");
			toClient.write(buffer);
			toClient.flush();

			url = "ok";
		} catch (IOException ex) {
			arg2.setAttribute("returnSting", "服务器忙，请稍后下载！");
			ex.printStackTrace();
			return arg0.findForward("error");
		} catch (NullPointerException ex) {
			return null;
		} catch (Exception ex) {
			arg2.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return arg0.findForward("error");
		} finally {
			fis.close();
			toClient.close();
		}
		if ("ok".equals(url)) {
			return null;
		} else {
			arg2.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return arg0.findForward(url);
		}
	}

	/**
	 * 只读查询发文内容
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward selectonedoc(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("selectonedoc");
	}

	/**
	 * 进入归档
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward guidangform(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("guidangform");
	}

	/**
	 * 进入修改发文内容
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward guidang(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		File dir = new File("D:\\guidang\\xingzhengshouwen\\");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		/**
		 * 创建归档文件目录
		 */
		String year = String.valueOf(xingzhengshouwenNeirong.getShouwenriqi()
				.getYear() + 1900);
		String month = String.valueOf(xingzhengshouwenNeirong.getShouwenriqi()
				.getMonth() + 1);
		String number = xingzhengshouwenNeirong.getShouwenbianhao();
		File fileDir = new File("D:\\guidang\\xingzhengshouwen\\" + year + "\\"
				+ month + "\\" + number + "\\");
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}

		/**
		 * 拷贝附件
		 */
		if (xingzhengshouwenNeirong.getTXingzhengshouwenFujians().size() > 0) {
			List<TXingzhengshouwenFujian> fuJianList = new ArrayList<TXingzhengshouwenFujian>(
					xingzhengshouwenNeirong.getTXingzhengshouwenFujians());
			for (TXingzhengshouwenFujian xingzhengfawenFujian : fuJianList) {
				String oldPath = xingzhengfawenFujian.getFujianAddress();
				String newPath = "D:\\guidang\\xingzhengshouwen\\" + year
						+ "\\" + month + "\\" + number + "\\"
						+ xingzhengfawenFujian.getFujianName();
				FileManage.copyFile(oldPath, newPath);
			}

		}

		/**
		 * 生成承办单文件
		 */
		String html = arg2.getParameter("html");
		FileManage fileManage = new FileManage();
		fileManage.newFile("D:\\guidang\\xingzhengshouwen\\" + year + "\\"
				+ month + "\\" + number + "\\" + "承办单.html", html);
		xingzhengshouwenNeirong.setIsguidang(1);
		xingzhengshouwenNeirongBO
				.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
		arg2.setAttribute("linkurl",
				"/documentationflow/xingzhengshouwen/adminlist.jsp");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}

	/**
	 * 只读查询发文内容
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward print(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("print");
	}

	/**
	 * 只读查询发文内容
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward adminselectonedoc(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		return arg0.findForward("adminselectonedoc");
	}

	/**
	 * 添加经手人
	 * 
	 * 
	 * @param request
	 * @param docid
	 */
	private void addJingShouRen(HttpServletRequest request, String docid) {
		TXingzhengshouwenNeirong xingzhengshouwenNeirong;
		xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(docid);
		if (xingzhengshouwenNeirong.getJingshouren() == null) {
			xingzhengshouwenNeirong.setJingshouren(","
					+ SessionUser.getUser(request).getUser_id() + ",");
			xingzhengshouwenNeirong.setJingshoubumen(SessionUser.getUser(
					request).getDepartment());
		} else {
			xingzhengshouwenNeirong.setJingshouren(xingzhengshouwenNeirong
					.getJingshouren()
					+ SessionUser.getUser(request).getUser_id() + ",");
			xingzhengshouwenNeirong.setJingshoubumen(xingzhengshouwenNeirong
					.getJingshoubumen()
					+ SessionUser.getUser(request).getDepartment() + ",");
		}
		xingzhengshouwenNeirongBO
				.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
	}

	/**
	 * 中止一个流程
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward suspend(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long flowid = Long.valueOf(arg2.getParameter("flowid"));
		if (workFlowTemplate.processInstanceSuspend(flowid)) {
				JSONObject object = new JSONObject();
				object.put("success", true);
				arg3.getWriter().print(object);
		} else {
			JSONObject object = new JSONObject();
			object.put("success", false);
			arg3.getWriter().print(object);
		}
		return null;
	}

	/**
	 * 完结一个流程
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward end(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		try {
			WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
			Long flowid = Long.valueOf(arg2.getParameter("flowid"));
			workFlowTemplate.processInstanceEnd(flowid);
			List<TaskInstance> tasklist = new ArrayList<TaskInstance>(
					workFlowTemplate.selectunfinishedTasks(flowid));
			for (TaskInstance taskInstance : tasklist) {
				JbpmConfiguration jbpmConfiguration = JbpmConfiguration
						.getInstance();
				JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
				try {
					Session hSession = jbpmContext.getSession();
					taskInstance = jbpmContext.loadTaskInstance(taskInstance
							.getId());
					// 删除对应的所有日志信息

					Query deleteLogQuery = hSession
							.createSQLQuery("delete from JBPM_LOG where 1=1 or TOKEN_ = ? or TASKINSTANCE_ = ?");
					deleteLogQuery.setParameter(0, taskInstance.getId());
					deleteLogQuery.setParameter(1, taskInstance.getToken()
							.getId());
					deleteLogQuery.executeUpdate();

					// 删除任务实例
					Query deleteTaskInstanceQuery = hSession
							.createSQLQuery("delete from JBPM_TASKINSTANCE where ID_ = ?");
					deleteTaskInstanceQuery.setParameter(0,
							taskInstance.getId());
					deleteTaskInstanceQuery.executeUpdate();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				} finally {
					jbpmContext.close();
				}
			}
			if (workFlowTemplate.processInstanceEnd(flowid)) {
					JSONObject object = new JSONObject();
					object.put("success", true);
					arg3.getWriter().print(object);
			} else {
				JSONObject object = new JSONObject();
				object.put("success", false);
				arg3.getWriter().print(object);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除一个流程
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		Long flowid = Long.valueOf(xingzhengshouwenNeirong.getFlowid());
		xingzhengshouwenNeirongBO
				.deleteXingzhengshouwen(xingzhengshouwenNeirong);
		if (workFlowTemplate.processInstanceSuspend(flowid)) {
				JSONObject object = new JSONObject();
				object.put("success", true);
				arg3.getWriter().print(object);
		} else {
			JSONObject object = new JSONObject();
			object.put("success", false);
			arg3.getWriter().print(object);
		}
		return null;
	}

	/**
	 * 重启一个流程
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward resume(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long flowid = Long.valueOf(arg2.getParameter("flowid"));
		if (workFlowTemplate.processInstanceResume(flowid)) {
				JSONObject object = new JSONObject();
				object.put("success", true);
				arg3.getWriter().print(object);
		} else {
			JSONObject object = new JSONObject();
			object.put("success", false);
			arg3.getWriter().print(object);
		}
		return null;
	}

	/**
	 * 重启一个流程
	 * 
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward cuiban(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		String id = arg2.getParameter("id");
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(id);
		xingzhengshouwenNeirong.setIschuiban(1);
		xingzhengshouwenNeirongBO
				.updateTXingzhengshouwenneirong(xingzhengshouwenNeirong);
		JSONObject object = new JSONObject();
		object.put("success", true);
		arg3.getWriter().print(object);
		return null;
	}

	/**
	 * 改变任务接收人
	 * 
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward changActor(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long taskid = Long.valueOf(arg2.getParameter("taskid"));
		String actorid = arg2.getParameter("actorid");
		if (workFlowTemplate.changTaskActor(taskid, actorid)) {
			JSONObject object = new JSONObject();
			object.put("success", true);
			arg3.getWriter().print(object);
		} else {
			JSONObject object = new JSONObject();
			object.put("success", false);
			arg3.getWriter().print(object);
		}
		return null;
	}

	/**
	 * 删除任务
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteTask(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long taskid = Long.valueOf(arg2.getParameter("taskid"));

		if (xingzhengshouwenNeirongBO.deleteTask(Integer.parseInt(Long.valueOf(
				taskid).toString()))) {
			JSONObject object = new JSONObject();
			object.put("success", true);
			arg3.getWriter().print(object);
		} else {
			JSONObject object = new JSONObject();
			object.put("success", false);
			arg3.getWriter().print(object);
		}
		return null;
	}

	/**
	 * 查询来文单位
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward selectlaiwendanwei(ActionMapping arg0,
			ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		String laiwendanwei = ToolKit
				.getProperties(
						"com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource_zh_CN.properties",
						this).getProperty("xingzhengshouwen.laiwendanwei");
		String[] laiwendanweiarray = laiwendanwei.split(",");
		JSONArray array = new JSONArray();
		for (String danwei : laiwendanweiarray) {
			JSONObject danweiJSON = new JSONObject();
			danweiJSON.put("text", danwei);
			danweiJSON.put("value", danwei);
			array.put(danweiJSON);
		}
		arg3.getWriter().print(array);
		return null;
	}

	/**
	 * 数字签名验证列表
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward yanzhenglist(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		String path = arg2.getContextPath();
		String basePath = arg2.getScheme() + "://" + arg2.getServerName() + ":"
				+ arg2.getServerPort() + path + "/";
		JSONArray array = new JSONArray();
		String id = arg2.getParameter("id");
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(id);
		Set<TXingzhengshouwenYijian> yijianset = xingzhengshouwenNeirong
				.getTXingzhengshouwenYijians();
		for (TXingzhengshouwenYijian xingzhengshouwenYijian : yijianset) {
			if ("传阅".equals(xingzhengshouwenYijian.getFlowName())) {
				JSONObject object = new JSONObject();
				object.put("username",
						xingzhengshouwenYijian.getExamineUserName());
				object.put("datetime", ToolKit
						.FormatDate(xingzhengshouwenYijian.getExamineDate()));
				object.put(
						"operate",
						"<a href='"
								+ basePath
								+ "tjsinfo/tjsoa/xingzhengshouwen/shenpi.do?method=yanzheng&id="
								+ id + "&yijianid="
								+ xingzhengshouwenYijian.getId() + "'>验证</a>");
				array.put(object);
			}
		}
		arg3.getWriter().print(array);
		return null;
	}

	/**
	 * 数字验证
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward yanzheng(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		TXingzhengshouwenNeirong xingzhengshouwenNeirong = xingzhengshouwenNeirongBO
				.selectOneById(arg2.getParameter("id"));
		arg2.setAttribute("xingzhengshouwenNeirong", xingzhengshouwenNeirong);
		TXingzhengshouwenYijian xingzhengshouwenYijian = xingzhengshouwenYijianBO
				.selectById(arg2.getParameter("yijianid"));
		arg2.setAttribute("xingzhengshouwenYijian", xingzhengshouwenYijian);
		return arg0.findForward("yanzheng");
	}

	public ITXingzhengshouwendengjibuBO getXingzhengshouwendengjibuBO() {
		return xingzhengshouwendengjibuBO;
	}

	public void setXingzhengshouwendengjibuBO(
			ITXingzhengshouwendengjibuBO xingzhengshouwendengjibuBO) {
		this.xingzhengshouwendengjibuBO = xingzhengshouwendengjibuBO;
	}

	/**
	 * 删除文件和待办
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteDoc(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		String id = arg2.getParameter("id");
		Integer taskId = Integer.parseInt(arg2.getParameter("taskid"));
		xingzhengshouwenNeirongBO.deleteTask(Integer.parseInt(Long
				.valueOf(taskId).toString()));
		xingzhengshouwenNeirongBO
				.deleteXingzhengshouwen(xingzhengshouwenNeirongBO
						.selectOneById(id));
		arg2.setAttribute("linkurl",
				"tjsinfo/tjsoa/xingzhengshouwen/shouwenadmin.do");
		arg2.setAttribute("message",
				"com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.message");
		return arg0.findForward("success");
	}
	
	public ActionForward getDengjiNumber(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		
		String typeid = arg2.getParameter("typeid");
		String shouwentype="";
		if("jcc".equals(typeid)){
			shouwentype="津仓储";
		}else{
			shouwentype="行"+typeid+"字";
		}
		Date date = new Date();
		Integer year = date.getYear() + 1900;
		shouwentype += String.valueOf(year);
		Integer number = xingzhengshouwenNeirongBO.selectMaxZongShouWenHaoByYear(shouwentype);
		
			JSONObject object = new JSONObject();
			object.put("number", number);
			object.put("shouwentype",shouwentype);
			arg3.getWriter().print(object);
		return null;
	}
	
	/**
	 * 跟踪
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward genzong(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long flowid = Long.valueOf(arg2.getParameter("flowid"));
		Integer unfinshtasksize  = workFlowTemplate.selectunfinishedTasksSize(flowid);
		List<Taskinstance> list;
		JSONArray array = new JSONArray();
		list = taskInstanceBO.selectAllTask(flowid);
		
		Comparator comp = new Comparator(){
	          public int compare(Object o1,Object o2) {
	        	  Taskinstance p1=(Taskinstance)o1;
	        	  Taskinstance p2=(Taskinstance)o2; 
	             if(p1.getCreate().after(p2.getCreate()))
	                 return 1;
	             else
	                 return 0;
	             }
	        };
	        Collections.sort(list, comp);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Taskinstance taskinstance = (Taskinstance) iterator.next();
			JSONObject taskObject = new JSONObject();
			if ("传阅".equals(taskinstance.getName())) {
				taskObject.put("taskname", "批阅");
			}else{
				taskObject.put("taskname", taskinstance.getName());
			}
			
			taskObject.put("username", taskinstance.getTjsUser().getUserName());
			taskObject.put("time", ToolKit.getFormatDate(taskinstance.getEnd(), "yyyy-MM-dd HH:mm"));
			if(taskinstance.getEnd() ==null&& unfinshtasksize > 1 && taskinstance.getIssub() == null)
			{
				taskObject.put("operate", "<a href='javascript:selectuserradiowinshow("+taskinstance.getId()+",userid,username,node)'>修改候选人</a>&nbsp;&nbsp;<a href='javascript:endTask("+taskinstance.getId()+")'>结束</a>");
			}
			else if(taskinstance.getEnd() ==null)
			{
				taskObject.put("operate", "<a href='javascript:selectuserradiowinshow("+taskinstance.getId()+",userid,username,node)'>修改候选人</a>");
			}
			else
			{
				taskObject.put("operate", "");
			}
			array.put(taskObject);
		}
		arg3.getWriter().print(array);
		return null;
	}

}
